<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2021/5/8
 * Time: 16:12
 */

namespace App\Services;


use App\Admin\Actions\Import\PatentEstimateImportTool;
use App\Extensions\Exporters\PatentEstimateExporter;
use App\Extensions\Renderable\PatentDetailRenderAble;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Tools\BatchActions;

class PatentEstimateService
{
    public function gridColumn(Grid $grid)
    {
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->model()->with(['fees'])->where('state', '>', 0)->where('score','>',0);
        $grid->column('patent_sn', __('专利号'));
        $grid->column('patent_name', __('专利名称'))->modal('详情', PatentDetailRenderAble::class);
        $grid->column('patent_type_id', __('专利类型'))->using(patent_type_select_option());
        $grid->column('apply_date', __('申请日'));
        $grid->column('inventor', __('发明人'));
        //$grid->column('legal_status', __('法律状态'));

        $grid->batchActions(function (BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->tools(function(Grid\Tools $tools){
            $tools->append(new PatentEstimateImportTool());
        });
        $grid->column('score', '价值评估')->display(function ($score) {
            if($score <= 0){
                return '';
            }
            $url = admin_route('fjfl.show', [$this->id]);
            return <<<HTML
<a  href="{$url}"  target="_blank">
    <span style="color: #ff8a27;line-height: 20px;font-size: 1.8rem;">
        <span style="font-size: 2.4rem;">{$score}</span>分
    </span>
</a>
HTML;
        });
        $grid->filter(function(Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->column(1/2,function(Grid\Filter $filter){
                $filter->where(function ($query) {
                    $query->where('patent_sn', 'like', "%{$this->input}%")
                        ->orWhere('patent_name', 'like', "%{$this->input}%");
                }, '关键字')->placeholder('专利号/专利名');
            });
            $filter->column(1/2,function(Grid\Filter $filter){
                $filter->equal('patent_type_id','专利类型')->radio(patent_type_select_option());
            });
        });
        $grid->exporter(new PatentEstimateExporter());
        return $grid;
    }
    public function showHtml($patent)
    {
        if(!$patent['assessment']){
            return response('',404);
        }
        $data = [];
        foreach ($patent['assessment'] as $key=>$item){
            $item['score']= round($item['score'],2);
            $data[$key] = $item;
        }
        return view('patent-pg-detail')->with([
            'patent' => $patent,
            'gk' => [
                'data' => [
                    '法律价值度' => $data['fl']['score'],
                    '技术价值度' => $data['js']['score'],
                    '经济价值度' => $data['jj']['score'],
                ],
                'score' => $patent['score'],
            ],
            'fl' => $data['fl'],
            'js' => $data['js'],
            'jj' =>$data['jj'],
        ]);
    }

}
